﻿using System;
using System.IO;
using System.Web;
using Common.Logging;
using TSharp.Core.Osgi;

namespace TSharp.Core.Mvc.Web
{
    /// <summary>
    ///     mvc应用程序基类
    ///     <para>2010/9/16</para>
    ///     <para>TANGJINGBO</para>
    ///     <author>tangjingbo</author>
    /// </summary>
    public class TSharpHttpApplication : HttpApplication
    {
        private ILog log = LogManager.GetCurrentClassLogger();
        /// <summary>
        ///     Gets the ext point engine.
        /// </summary>
        public OsgiEngine ExtensionPointEngine { get; private set; }

        public override void Init()
        {
            base.Init();
            this.Error += TSharpHttpApplication_Error;
        }

        void TSharpHttpApplication_Error(object sender, EventArgs e)
        {
            var ex = Server.GetLastError();
            Server.ClearError();
            log.Error("发生未处理异常！", ex);
        }

        /// <summary>
        ///     Application_s the start.
        /// </summary>
        protected virtual void Application_Start(object sender, EventArgs args)
        {
            ExtensionPointEngine = OsgiEngine.InitWebEngine();
        }

        /// <summary>
        ///     Application_s the stop.
        /// </summary>
        protected void Application_End(object sender, EventArgs eventArgs)
        {
            using (ExtensionPointEngine)
            {
            }
        }
    }
}